package com.fan.teamer.process;

import java.util.Date;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.model.domain.TeamMember;
import com.fan.teamer.model.domain.TeamMemberId;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

public class TeamCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("TeamAddProcess");

	private String name;
	private String description;
	private String sport;
	private String icon;
	private String country;
	private String state;
	private String city;
	private String area;
	private String timezone;
	private String gender;
	private String ageProfile;
	private Date createdTime;
	private String attribute;

	private Member operator;

	private TeamService teamService;
	private TeamMemberService teamMemberService;
	private MemberService memberService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TeamCreateProcess.logger = logger;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSport() {
		return sport;
	}

	public void setSport(String sport) {
		this.sport = sport;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getTimezone() {
		return timezone;
	}

	public void setTimezone(String timezone) {
		this.timezone = timezone;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAgeProfile() {
		return ageProfile;
	}

	public void setAgeProfile(String ageProfile) {
		this.ageProfile = ageProfile;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public int initCheck() {
		if (teamService == null || teamMemberService == null
				|| memberService == null) {
			setErrorMessage("Service cannot be null");
			return TeamerGlobalVariables.FAILED;
		}
		if (operator == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;

	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		// create team
		Team team = new Team();

		team.setName(name);
		team.setCountry(country);
		team.setInitiator(operator.getEmail());

		team.setDescription(description);
		team.setSport(sport);
		team.setIcon(icon);

		team.setState(state);
		team.setCity(city);
		team.setArea(area);
		team.setTimezone(timezone);
		team.setGender(gender);
		team.setAgeProfile(ageProfile);
		team.setCreatedTime(new Date(System.currentTimeMillis()));
		team.setAttribute(attribute);

		try {
			teamService.addTeam(team);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Team " + name + " cannot be saved correctly");
			return TeamerGlobalVariables.FAILED;
		}

		// add operator to team
		TeamMember teamMember = new TeamMember();
		TeamMemberId teamMemberId = new TeamMemberId();
		teamMemberId.setMemberEmail(operator.getEmail());
		teamMemberId.setTeamId(team.getId());
		teamMember.setId(teamMemberId);

		teamMember.setMemberRole(TeamerGlobalVariables.ORGANIZER);

		try {
			teamMemberService.addTeamMember(teamMember);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("TeamMember " + operator.getEmail()
					+ " cannot be saved correctly");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
